/**
 * Created by evantang on 2018/3/29.
 */

import {Component, ElementRef, OnInit} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {Subject} from "rxjs/Subject";
import {CCRiskControllerService} from "../../../service/CCRiskController";
import {DialogService} from "../../../service/dialogService";
import {FileService} from "../../../service/fileService";
import {WeUINavBarItem} from "angular4-weui";
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
import {UICCDimensionCodeAwareTaskCriteria} from "../../../entity/UICCDimensionCodeAwareTaskCriteria";
import {RestDimensionCodeAwareTask} from "../../../entity/RestDimensionCodeAwareTask";
import {BaseAuthenticateComponent} from "../../../shared/component/baseAuthenticate.component";
@Component({
  selector: 'app-risk-check-dimension',
  templateUrl: './risk-check-dimension.component.html'
})
export class RiskCheckDimensionComponent extends BaseAuthenticateComponent implements OnInit {

  activeValue = '1';

  uiCriteria: UICCDimensionCodeAwareTaskCriteria = new UICCDimensionCodeAwareTaskCriteria();
  valuesForRiskCheckTaskList: RestDimensionCodeAwareTask[] = [];
  searchStream = new Subject<string>();
  totalRecord: number;

  code: string;
  remindTxt = '';
  loadMore = false;
  skipFlag = 1;

  constructor(protected a_router: Router, public element: ElementRef, public fileService: FileService,
              public route: ActivatedRoute, public _dialogService: DialogService, public riskService: CCRiskControllerService) {
    super(a_router, element, route, true, _dialogService);
    this.route.params.subscribe(params => {
      this.code = params['code'];
    });
  }

  ngOnInit() {

    this.uiCriteria.pagesize = 10;
    this.uiCriteria.skip = 0;
    this.uiCriteria.dimensionCode = this.code;
    this.uiCriteria.status = 'processing';

    this.searchStream
      .pipe(
        debounceTime(500),     // wait for 500ms pause in events
        distinctUntilChanged() // ignore if next search term is same as previous
      )
      .subscribe((term: string) => {
        this.searchRiskTask(term);
      });

    this.refreshRiskTaskList();

  }

  refreshRiskTaskList(cb?: () => void) {
    this.riskService.dimensionTaskList(this.uiCriteria).subscribe(
      (res) => {
        this.valuesForRiskCheckTaskList = res.result;
        this.totalRecord = res.totalRecord;

        this.loadMore = this.totalRecord > this.valuesForRiskCheckTaskList.length;

        if (!this.valuesForRiskCheckTaskList || this.valuesForRiskCheckTaskList.length === 0) {
          this.remindTxt = '暂无数据';
        } else {
          this.remindTxt = this.valuesForRiskCheckTaskList.length + " / " + this.totalRecord;
        }
      },
      err => this.handleError(err)
    );

  }


  detail(event: RestDimensionCodeAwareTask) {
    this.a_router.navigate(["/dashboard/task", event.task.id, "detail", "scan", this.code]);
  }

  onScroll() {
    this.uiCriteria.skip = this.uiCriteria.pagesize * this.skipFlag;
    this.riskService.dimensionTaskList(this.uiCriteria).subscribe(
      res => {
        this.valuesForRiskCheckTaskList = this.valuesForRiskCheckTaskList.concat(res.result)
        if (this.totalRecord > this.valuesForRiskCheckTaskList.length) {
          this.loadMore = true;
          // this.remindTxt = "查看更多";
          this.skipFlag++;
        } else {
          this.loadMore = false;
          // this.remindTxt = "数据已加载完";
        }
        this.remindTxt = this.valuesForRiskCheckTaskList.length + " / " + this.totalRecord;
      }
    );
  }

  onActivated(item: WeUINavBarItem): void {
    // 恢复滚动条的垂直位置,从头开始
    // this.resetScroll();
    this.activeValue = item.value;
    switch (this.activeValue) {
      case '1':
        this.getProcessingTask();
        break;
      case '2':
        this.getCompletedTask();
        break;
    }

  }

  getProcessingTask() {
    this.uiCriteria.status = 'processing';
    this.refreshRiskTaskList();
  }

  getCompletedTask() {
    this.uiCriteria.status = 'finished';
    this.refreshRiskTaskList();
  }

  searchRiskTask(value: string) {
    this.refreshRiskTaskList();
  }

  search(value: string): void {
    this.searchStream.next(value);
  }
}
